package org.lds.message.center.admin.business.template.converter;

import org.lds.message.center.admin.business.app.entity.App;
import org.lds.message.center.admin.business.app.mapper.AppMapper;
import org.lds.message.center.admin.business.template.entity.Template;
import org.lds.message.center.admin.business.template.model.response.TemplateBaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Objects;


@Component
public class TemplateToBaseResponseConverter implements Converter<Template, TemplateBaseResponse> {

    @Autowired
    private AppMapper appMapper;

    @Override
    public TemplateBaseResponse convert(Template source) {
        App app = appMapper.selectById(source.getAppId());
        return TemplateBaseResponse.builder()
                .id(source.getId())
                .appId(source.getAppId())
                .appName(Objects.isNull(app) ? null : app.getName())
                .name(source.getName())
                .desc(source.getDesc())
                .enable(source.getEnable())
                .subject(source.getSubject())
                .text(source.getText())
                .creator(source.getCreator())
                .modifier(source.getModifier())
                .createTime(source.getCreateTime())
                .updateTime(source.getUpdateTime())
                .build();
    }

}


